package vmcs.entity;

import vmcs.entity.Coin;
import vmcs.observer.StoreItemChangeManager;
import vmcs.observer.StoreItemChangeManagerRegistry;
import vmcs.utility.ReflectionUtils;

/**
 * Title:        VMCS
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:      ISS
 * @author Pang Pingli
 * @version 1.0
 */
public class CashStoreItem extends StoreItem {

    public CashStoreItem(Coin coin, int qty) {
        super((StoreObject) coin, qty);
        setQuantity(qty);
    }

    // Chain of responsibility 
    StoreItem checkWeight(double weight) {
        Coin coin = (Coin) this.getContent();
        if (coin.getWeight() == weight) {
            return this;
        } else {
            if (nextItem != null) {

                return ((CashStoreItem) nextItem).checkWeight(weight);
            } else {
                return null;
            }
        }
    }
    // OBSERVER IMPL BEGIN

    public void decreaseQuantityByOne() {
        if(this.quantity > 0){
            setQuantity(this.quantity-1);
        }
    }
    
    public void increaseQuantityByOne() {
        if(this.quantity >= 0){
            setQuantity(this.quantity+1);
        }
    }
    
    protected StoreItemChangeManager getStoreItemChangeManager() {
        return StoreItemChangeManagerRegistry.getManager(StoreItemChangeManagerRegistry.StoreItemType.Cash);
    }

    @Override
    public void setQuantity(int quantity) {
         this.quantity = quantity;

        // OBSERVER IMPL BEGIN

        System.out.println("CashStoreItem: Quantity set to " + quantity + " for store item " + this);

        // get change manager for this store item
        StoreItemChangeManager manager = getStoreItemChangeManager();

        // notify interested observers
        manager.notify(this, StoreItemChangeManager.EVENT_QUANTITYCOINCHANGE);
        // OBSERVER IMPL END
    }

    // OBSERVER IMPL END
    @Override
    public String toString() {
        return ReflectionUtils.toString(this);
    }
}